package com.xbongbong.paas.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.xbongbong.paas.pojo.dto.FormDataListDTO;
import com.xbongbong.paas.pojo.vo.PaasFormDataEsListVO;
import com.xbongbong.paas.service.PaasFormDataService;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.thread.TransmittableThreadLocalUtil;
import com.xbongbong.paas.toolbox.wrap.XbbResponse;
import com.xbongbong.pro.common.pojo.dto.CheckRepeatDTO;
import com.xbongbong.pro.common.pojo.dto.CustomerCheckRepeatDTO;
import com.xbongbong.pro.common.pojo.vo.CheckRepeatVO;
import com.xbongbong.pro.common.pojo.vo.ClueCheckRepeatVO;
import com.xbongbong.pro.common.pojo.vo.CustomerCheckRepeatVO;
import com.xbongbong.pro.constant.XbbProConstant;
import com.xbongbong.pro.enums.DistributorMarkEnum;
import com.xbongbong.pro.enums.SaasMarkEnum;
import com.xbongbong.pro.enums.errorcodes.SystemErrorCodeEnum;
import com.xbongbong.saas.enums.XbbRefTypeEnum;
import com.xbongbong.saas.service.CommonService;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Objects;

/**
 * @author feng.zheng
 * @version v1.0
 * @date 2019/1/31 15:44
 * @since v1.0
 */
@RestController
@RequestMapping(XbbProConstant.API_ROOT_PREFIX + "/common")
public class CommonController {
    @Resource
    private CommonService commonService;
    @Resource
    private PaasFormDataService paasFormDataService;

    @RequestMapping(value = "/checkRepeat", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String checkRepeat(@RequestBody @Valid CheckRepeatDTO checkRepeatDTO, BindingResult br) throws Exception {
        XbbResponse<CheckRepeatVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                CheckRepeatVO checkRepeatVO = commonService.checkRepeat(checkRepeatDTO);
                response = new XbbResponse<>(checkRepeatVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    @RequestMapping(value = "/customerCheckRepeat", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String customerCheckRepeat(@RequestBody @Valid CustomerCheckRepeatDTO customerCheckRepeatDTO, BindingResult br) throws Exception {
        XbbResponse<CustomerCheckRepeatVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransmittableThreadLocalUtil.setValue(SaasMarkEnum.SAAS.getCode(), XbbRefTypeEnum.CUSTOMER_MANAGEMENT.getCode(), null, customerCheckRepeatDTO.getDistributorMark());
                CustomerCheckRepeatVO customerCheckRepeatVO = commonService.customerCheckRepeat(customerCheckRepeatDTO);
                response = new XbbResponse<>(customerCheckRepeatVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    @RequestMapping(value = "/clueCheckRepeat", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String clueCheckRepeat(@RequestBody @Valid CustomerCheckRepeatDTO customerCheckRepeatDTO, BindingResult br) throws Exception {
        XbbResponse<ClueCheckRepeatVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransmittableThreadLocalUtil.setValue(SaasMarkEnum.SAAS.getCode(), XbbRefTypeEnum.CLUE.getCode(), null, DistributorMarkEnum.OTHER.getCode());
                ClueCheckRepeatVO clueCheckRepeatVO = commonService.clueCheckRepeat(customerCheckRepeatDTO);
                response = new XbbResponse<>(clueCheckRepeatVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public String list(@RequestBody @Valid FormDataListDTO formDataListDTO, BindingResult br) throws Exception {
        XbbResponse<PaasFormDataEsListVO> response;
        if (br.hasErrors()) {
            String errMsg = Objects.requireNonNull(br.getFieldError()).getDefaultMessage();
            response = new XbbResponse<>(SystemErrorCodeEnum.API_ERROR_100002, errMsg);
        } else {
            try {
                TransmittableThreadLocalUtil.setValue(formDataListDTO.getSaasMark(), formDataListDTO.getBusinessType(), formDataListDTO.getMenuId(), formDataListDTO.getDistributorMark());
                PaasFormDataEsListVO paasFormDataEsListVO = paasFormDataService.listByCloudCode(formDataListDTO);
                response = new XbbResponse<>(paasFormDataEsListVO);
            } catch (XbbException e) {
                response = new XbbResponse<>(e);
            }
        }
        return JSON.toJSONString(response, SerializerFeature.DisableCircularReferenceDetect);
    }
}
